package com.esa.servlet.user;

import java.io.IOException;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esa.dbi.AccountDBI;
import com.esa.dbi.impl.AccountDBImpl;
import com.esa.javabean.Account;


public class LoginServlet extends HttpServlet {
	
	 
	 public void doGet(HttpServletRequest request, HttpServletResponse response)
	   throws ServletException, IOException {
	  doPost(request,response);
	 }

	 public void doPost(HttpServletRequest request, HttpServletResponse response)
	   throws ServletException, IOException {
	  
	  String acname = request.getParameter("acname");
	  String password = request.getParameter("password");
	  
	  HttpSession session=request.getSession(true);
	  
	  session.setAttribute("acname", acname);
	  
	  AccountDBI adbi= new AccountDBImpl();
	  Account acnt=new Account();
	  acnt.setAcname(acname);
	  acnt.setPassword(password);
	  boolean b = false;
	  //b = adbi.validate(acnt);
	  try {
		  b = adbi.validate(acnt);
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
	  String str=adbi.getAccount(acname).getCreateTime();
	  
	  if(b == true){
		  if(str.equalsIgnoreCase("0")){  
		  response.sendRedirect("ShowMyInfoServlet");
		  }
		  else /*if(adbi.getAccount(acname).getLevel().equalsIgnoreCase("1"))*/{
			response.sendRedirect("ShowAllStudentsServlet");
		  }
	  }else{
	   request.getRequestDispatcher("loginerror.jsp").forward(request, response);
	  }
	  
	 }
}
